﻿using System;
using System.Data.Linq;
using System.Linq;
using WebUI.DAL;
using System.Collections.Generic;

namespace WebUI.BL.Display
{
    public class DisplayBL
    {
        protected void clsDisplayInsert(tbl_DisplayBanner displays)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    tbl_DisplayBanner display = new tbl_DisplayBanner();
                    display.BannerCode = displays.BannerCode;
                    display.BannerName = displays.BannerName;
                    display.Tooltip = displays.Tooltip;
                    display.TextDescription = displays.TextDescription;
                    display.Img = displays.Img;
                    display.Thumb = displays.Thumb;
                    display.Link = displays.Link;
                    display.IndexShow = displays.IndexShow;
                    display.UpdatedDate = DateTime.Now;
                    display.UpdatedUser = displays.UpdatedUser;
                    display.Status = displays.Status;

                    adlerContextDb.tbl_DisplayBanners.InsertOnSubmit(display);
                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        protected void clsDisplayModify(tbl_DisplayBanner displays)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    var display = adlerContextDb.tbl_DisplayBanners.Single(d => d.BannerNo == displays.BannerNo);
                    display.BannerCode = displays.BannerCode;
                    display.BannerName = displays.BannerName;
                    display.Tooltip = displays.Tooltip;
                    display.TextDescription = displays.TextDescription;
                    display.Img = displays.Img;
                    display.Thumb = displays.Thumb;
                    display.Link = displays.Link;
                    display.IndexShow = displays.IndexShow;
                    display.UpdatedDate = DateTime.Now;
                    display.UpdatedUser = displays.UpdatedUser;
                    display.Status = displays.Status;

                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        protected void clsDisplayDelete(tbl_DisplayBanner displays)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    var display = adlerContextDb.tbl_DisplayBanners.Single(d => d.BannerNo == displays.BannerNo);
                    display.Status = displays.Status;

                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        // Show Display.
        public List<MODEL_SP_ADMIN_SEARCH_DISPLAYBANNER> Get_Admin_Displays(string bannername, string status)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                int index = 1;
                //var query = from n in contextdb.SP_ADMIN_SEARCH_DISPLAYBANNER(bannername, status)
                //            select new MODEL_SP_ADMIN_SEARCH_DISPLAYBANNER
                //            {
                //                Index = index++,
                //                BannerNo = n.BannerNo,
                //                BannerCode = n.BannerCode,
                //                BannerName = n.BannerName,
                //                Tooltip = n.Tooltip,
                //                TextDescription = n.TextDescription,
                //                Img = n.Img,
                //                Thumb = n.Thumb,
                //                Link = n.Link,
                //                IndexShow = n.IndexShow,
                //                UpdatedDate = n.UpdatedDate,
                //                UpdatedUser = n.UpdatedUser,
                //                Status = n.Status
                //            };
                //return query.ToList();
                return null;
            }
        }

        public tbl_DisplayBanner GetDisplaybyBannerno(int bannerno)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                return contextdb.tbl_DisplayBanners.Single(d => d.BannerNo == bannerno);
            }
        }
    }
}
